package com.sys.controller.common;

import com.sys.common.core.domain.AjaxResult;
import com.sys.common.utils.file.FileUploadUtils;
import com.sys.common.utils.file.FileUtils;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * 通用请求处理
 *
 * @author LGY
 */
@Slf4j
@Tag(name = "通用API")
@RestController
@RequestMapping("/common")
public class CommonController {

    /**
     * 自定义 Minio 服务器上传请求
     */
    @PostMapping("/uploadMinio")
    public AjaxResult uploadFileMinio(MultipartFile file) throws Exception {
        try {
            // 上传并返回新文件名称
            String fileName = FileUploadUtils.uploadMinio(file);
            AjaxResult ajax = AjaxResult.success();
            ajax.put("url", fileName);
            ajax.put("fileName", fileName);
            ajax.put("newFileName", FileUtils.getName(fileName));
            ajax.put("originalFilename", file.getOriginalFilename());
            return ajax;
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }
}
